/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author 
 */
public class GZIPPacker {
    private static final String helptext = "Benützung des Programmes:\n\tsimplezip -p <extension> <archivname>\n\tsimplezip -u <archivname>";

    public enum OPERATION {
        PACK, UNPACK
    }

    public static void perform(String[] args) {
        List<String> values = Arrays.asList(args);
        if (values.size() > 0 && values.size() < 4) {
            switchOperation(values);
        } else
            System.out.println(helptext);
    }

    private static void switchOperation(List<String> input) {
        String op = input.get(0).replaceAll("-", "");
        switch (op) {
            case "p":
                pack(input);
                break;
            case "u":
                unpack(input);
                break;
            case "?":
                System.out.println(helptext);
                break;
            default:
                System.err.println("Ungültiger Parameter 1. Möglich sind '-u', '-p' oder '-?'");
                break;
        }
    }

    private static void unpack(List<String> input) {
        if (input.size() < 2)
            System.out.println(helptext);
        else {
            String fileName = input.get(1);
            if (!fileName.toLowerCase().endsWith(".zip.gz"))
                fileName += ".zip.gz";
            File gz = new File("./" + fileName);

            File tempZip = new File("./data.zip");
            try (GZIPInputStream gi = new GZIPInputStream(new FileInputStream(gz))) {
                try (FileOutputStream fos = new FileOutputStream(tempZip, false)) {
                    int data;
                    while ((data = gi.read()) != -1)
                        fos.write(data);
                }
            } catch (IOException ex) {
                Logger.getLogger(GZIPPacker.class.getName()).log(Level.SEVERE, null, ex);
            }

            //extract files from ZIP
            try (ZipInputStream zi = new ZipInputStream(new FileInputStream(tempZip))) {
                ZipEntry ze;
                while ((ze = zi.getNextEntry()) != null) {
                    File outputFile = new File(ze.getName());
                    try (FileOutputStream fos = new FileOutputStream(outputFile, false)) {
                        int data;
                        while ((data = zi.read()) != -1)
                            fos.write(data);
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(GZIPPacker.class.getName()).log(Level.SEVERE, null, ex);
            }
            tempZip.delete();
        }
    }

    private static void pack(List<String> input) {
        if (input.size() < 3)
            System.out.println(helptext);
        else {
            final String packFileExt = input.get(1);
            FilenameFilter ff = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    if (packFileExt.equals("*"))
                        return true;
                    return name.toLowerCase().endsWith(packFileExt);
                }
            };

            File currentDir = new File("./");
            File[] fArr = currentDir.listFiles(ff);
            if (fArr.length > 0) {
                try {

                    //Files in ZIP-Archiv packen
                    File tempFile = new File("./data.zip");
                    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempFile, false))) {
                        for (File f : fArr) {
                            ZipEntry ze = new ZipEntry(f.getName());
                            zos.putNextEntry(ze);
                            try (FileInputStream in = new FileInputStream(f)) {
                                int data;
                                while ((data = in.read()) != -1) {
                                    zos.write(data);
                                }
                            }
                            zos.closeEntry();
                        }
                    }

                    //ZIP File gzippen
                    File gzipFile = new File("./" + input.get(2) + ".zip.gz");
                    try (FileInputStream in = new FileInputStream(tempFile)) {
                        try (GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(gzipFile, false))) {
                            int data;
                            while ((data = in.read()) != -1) {
                                gos.write(data);
                            }
                        }
                    }
                    tempFile.delete();
                } catch (IOException ex) {
                    Logger.getLogger(GZIPPacker.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Archiv wurde erstellt.");
            } else
                System.out.println("Es wird kein Archiv erstellt.");
        }
    }
}
